package com.jd.tuning.jvm.threadpool;

import java.util.Date;
import java.util.concurrent.*;

/**
 * 线程池3
 * //ForkJoinPool 实现1-100的求和
 */
public class ForkJoinPoolExecutorTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool forkJoinPool=new ForkJoinPool();
        ForkJoinTask<Integer> task=forkJoinPool.submit(new MyTask(1,100));
        Integer sum=task.get();
        System.out.println(sum);


    }
}

class MyTask extends  RecursiveTask<Integer>{
    //当前任务计算的起点
    private int start;
    //当前任务计算的结束
    private int end;
    //阈值,如果end-start的阈值以内，那么久不用再去细分任务
    public static  final int threshold=2;

    public MyTask(int start, int end) {
        this.start=start;
        this.end=end;
    }

    @Override
    protected Integer compute() {
        int sum=0;
        boolean needFork=(end-start)>threshold;
        if(needFork){
            int middle=(start+end)/2;
            MyTask leftTask=new  MyTask(start,middle);
            MyTask rightTask=new  MyTask(middle+1,end);
            //执行子任务
            leftTask.fork();
            rightTask.fork();

            //子任务执行完成之后的结果
            Integer leftResult=leftTask.join();
            Integer rightResult=rightTask.join();

            sum=leftResult+rightResult;
        }else{
                for(int i=start;i<=end;i++){
                        sum+=i;
                }
        }
        return sum;
    }
}
